package stud.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import stud.aistar.entity.IllMed;
import stud.aistar.service.IIllMedService;
import stud.aistar.service.IMedicineVoService;
import stud.aistar.vo.MedicineVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * *
 *
 * @ClassName :wangyu
 * @Description
 * @Author
 * @Date2019/10/4 20:26
 * @Version V1.0
 */
@Controller
@RequestMapping("/index")
public class MedicineVoController {

    @Autowired
    private IMedicineVoService medicineVoService;

    @Autowired
    private IIllMedService medService;


    @GetMapping("/medicine")
    public String index(){
        //转发到jsp页面...
        return "medicine/medicine";
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest req, Model model,  @RequestParam(defaultValue = "") String name,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize){

        List<IllMed> medList = medService.findAll();

        name = req.getParameter("name");
        //分页处理
        PageInfo<MedicineVo> pageInfo = medicineVoService.findAll(Integer.valueOf(pageNum),Integer.valueOf(pageSize),name);
        //req.setAttribute("pageInfo",pageInfo);
        model.addAttribute("pageInfo",pageInfo);

        model.addAttribute("medList",medList);
        //转发到medicineData.jsp;
        return "medicine/medicineData";
    }

    @GetMapping("/search")
    public String list (HttpServletRequest request,Model model){
      String medName = request.getParameter("medName");
        System.out.println(medName);

            List<MedicineVo> list = medicineVoService.findByMedName(medName);

            System.out.println(list);

            model.addAttribute("list", list);


        return "medicine/medSearch";
    }

    @GetMapping("med")
    public String med(HttpServletRequest request,Model model){
        String iId = request.getParameter("iId");

        model.addAttribute("iId",iId);
        return "medicine/illMeds";
    }

    @GetMapping("/illmed")
    public String illMed(@RequestParam String iId,Model model,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize){

        PageInfo<MedicineVo> pages = medicineVoService.finIlldAll(pageNum,pageSize,Integer.valueOf(iId));

        model.addAttribute("pages",pages);

        return "medicine/illMedicine";
    }

}
